<template>
  <div class="classi-fication">
    <!-- 分类管理 -->
    <!-- 操控区 -->
    <div class="handler-box">
      <el-button
        icon="el-icon-back"
        circle
        @click="backCategory"
        v-if="categoryLevel > 1"
      ></el-button>
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="small"
        @click="openDialog('')"
      >
        新增
      </el-button>
      <el-button
        type="danger"
        icon="el-icon-delete"
        size="small"
        @click="removeAll"
      >
        关闭订单
      </el-button>
    </div>

    <!-- 表格区 -->
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="changeSelects"
    >
      <el-table-column type="selection" min-width="5%" align="center">
      </el-table-column>
      <el-table-column prop="categoryName" label="分类名称" min-width="40%">
      </el-table-column>
      <el-table-column
        prop="categoryRank"
        label="排序值"
        min-width="10%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="添加时间"
        min-width="20%"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" min-width="25%" align="center">
        <template #default="{ row }">
          <el-link type="success" @click="openDialog(row.categoryId, row)"
            >修改</el-link
          >
          <el-link
            type="warning"
            @click="nextCategory(row.categoryId)"
            v-if="categoryLevel < 3"
            >下级分类</el-link
          >
          <el-popconfirm
            title="确定要删除本条数据吗？"
            @confirm="remove([row.categoryId])"
          >
            <el-link type="primary" slot="reference">删除</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区 -->
    <el-pagination
      background
      layout="prev, pager, next"
      align="center"
      :total="total"
      :page-size="pagesize"
      :current-page="page"
      :hide-on-single-page="true"
      @current-change="paginationChange"
      @prev-click="paginationChange"
      @next-click="paginationChange"
    >
    </el-pagination>

    <!-- 新增修改 -->

    <el-dialog
      width="50%"
      :title="categoryId ? `修改分类` : `添加分类`"
      :visible="visible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="closeDialog"
    >
      <el-form
        label-width="100px"
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
      >
        <el-form-item label="跳转连接" prop="categoryName">
          <el-input v-model.trim="ruleForm.categoryName"></el-input>
        </el-form-item>
        <el-form-item label="排序值" prop="categoryRank">
          <el-input-number
            v-model.trim="ruleForm.categoryRank"
            controls-position="right"
            :min="1"
            :max="200"
          >
          </el-input-number>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submit()">确认提交信息</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "classi-fication",
  data() {
    return {
      selects: [],
      parentId: 0,
      beforeCategoryId: 0,
      categoryLevel: 1,
      categoryId: "",
      visible: false,
      tableData: [],
      total: 0,
      pagesize: 10,
      page: 1,
      ruleForm: {
        categoryName: "",
        categoryRank: null,
      },
      rules: {
        categoryName: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
        categoryRank: [
          { required: true, message: "请输入排序值,200以内", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //获取数据
    async query() {
      try {
        let { resultCode, data } = await this.$api.ClassiFication.categories(
          this.page,
          this.pageSize,

          this.categoryLevel,
          this.parentId
        );

        if (+resultCode !== 200) {
          this.tableData = [];
          return;
        }

        let { totalCount, list } = data;
        this.tableData = list;
        console.log(this.tableData);
        this.total = totalCount;
      } catch (_) {
        this.tableData = [];
        this.total = 0;
      }
    },
    //下级分类
    nextCategory(categoryId) {
      this.categoryLevel++;
      this.beforeCategoryId = this.parentId;
      this.parentId = categoryId;
      this.query();
    },
    backCategory() {
      if (--this.categoryLevel === 1) {
        this.parentId = 0;
        this.query();
        return;
      }
      this.parentId = this.beforeCategoryId;
      this.query();
    },
    //打开新增/修改
    openDialog(categoryId = "", row) {
      this.visible = true;
      this.categoryId = categoryId;
      if (categoryId) {
        let { categoryName, categoryRank } = row;
        this.ruleForm = {
          categoryName,
          categoryRank,
        };
      } else {
        this.ruleForm.categoryName = "";
        this.ruleForm.categoryRank = "";
      }
    },
    //关闭新增
    closeDialog() {
      this.visible = false;
      this.categoryId = "";
      this.$refs["ruleForm"].resetFields();
    },
    //把勾选的放给selects
    changeSelects(val) {
      this.selects = val.map((item) => item.configId);
    },

    //批量删除
    async removeAll() {
      let selects = this.selects;
      if (selects.length == 0) {
        this.$message.error("请至少选择一项进行删除");
        return;
      }
      try {
        this.$confirm("此操作将永久删除选中项, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        this.remove(selects);
      } catch (_) {}
    },
    //删除
    async remove(ids) {
      let { resultCode } = await this.$api.ClassiFication.categoriesRemove(ids);
      if (+resultCode !== 200) {
        this.$message.error("删除失败，请稍后再试~~~");
        return;
      }
      this.$message.success("删除成功");
      if (this.page !== 1 && this.tableData.length === 1) {
        this.page--;
      }
      this.query();
    },
    //分页
    paginationChange(val) {
      this.page = +val;
      this.query();
    },
    //确认提交
    async submit() {
      try {
        await this.$refs["ruleForm"].validate();
        let sendFunc = this.$api.ClassiFication.categoriesInsert,
          categoryId = this.categoryId, //块级优化，不用每次都get,可以把this.去掉
          /*  { configName, goodsId, redirectUrl, configRank } = this.ruleForm, 
          data = {
            configName,
            configType: this.configType,
            goodsId,
            redirectUrl,
            configRank,
          };
         */
          data = { ...this.ruleForm };
        data.parentId = this.parentId;
        data.categoryLevel = this.categoryLevel;

        if (categoryId) {
          //修改
          sendFunc = this.$api.ClassiFication.categoriesUpdate;
          data.categoryId = this.categoryId;
        }
        let { resultCode } = await sendFunc(data);
        if (+resultCode !== 200) {
          this.$message.error("当前操作失败，请稍后再试");
          return;
        }
        this.$message.success("恭喜您，当前操作成功~");
        if (this.categoryId === "") this.page = 1;
        this.query();
        this.closeDialog();
      } catch (_) {}
    },
  },
  created() {
    this.query();
  },
};
</script>

<style lang="less" scoped>
/deep/.el-table .cell {
  font-size: 16px;
  font-weight: 500;
  color: #606266;
}

/deep/.el-table .el-table__cell {
  height: 100px;
  line-height: 100px;
}

/deep/.el-pagination {
  padding: 13px 5px;
  color: #303133;
  font-weight: 700;
}
/deep/.el-link--inner {
  margin-right: 10px;
}
</style>